﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Battleborn.Core.Abstract;
using Battleborn.Core.Concrete;
using Battleborn.Core.Models;

namespace Battleborn.Core.Repositories
{
    public class BattlebornRepository : IBattlebornRepository
    {
        private readonly BattlebornDb _db;

        private static readonly Func<BattlebornDb, string, Player> getPlayerById =
            (db, playerId) => db.Players.FirstOrDefault(p => p.PlayerId == playerId); 


        public BattlebornRepository(BattlebornDb db)
        {
            _db = db;
        }

        public IQueryable<Player> Players
        {
            get { return _db.Players; }
        }

        public IQueryable<Player> SearchPlayers(string name)
        {
            return _db.Players.Online().Where(p => p.Name.Contains(name));
        }

        public Player GetPlayerById(string playerId)
        {
            return getPlayerById(_db, playerId);
        }

        public void CommitChanges()
        {
            _db.SaveChanges();
        }

        public void Dispose()
        {
            _db.Dispose();
        }
    }
}
